const tagsView = {
  state: {
    visitedViews: JSON.parse(sessionStorage.getItem('visitedViews')) || [],
    cachedViews: JSON.parse(sessionStorage.getItem('cachedViews')) || [],
    oldVisitedViews: JSON.parse(sessionStorage.getItem('oldVisitedViews')) || [],
    oldCachedViews: JSON.parse(sessionStorage.getItem('oldCachedViews')) || [],
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      let index = state.visitedViews.findIndex(v => v.path === view.path);
      if (index != -1) state.visitedViews.splice(index, 1);
      state.visitedViews.push({
        name: view.name,
        path: view.path,
        title: view.meta.title || 'no-name',
        query: view.query
      })
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name)
      }
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, i + 1)
          break
        }
      }
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    },
    DEL_LEFT_VIEWS: (state, view) => {  
      const visitedViewsIndex = state.visitedViews.findIndex(v => v.path === view.path);  
      const cachedViewsIndex = state.cachedViews.findIndex(i => i === view.name);  
      
      if (visitedViewsIndex !== -1) {  
        state.visitedViews = state.visitedViews.slice(visitedViewsIndex);  
      }  
      
      if (cachedViewsIndex !== -1) {  
        state.cachedViews = state.cachedViews.slice(cachedViewsIndex);  
      }  
      
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews));  
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews));  
    },
    DEL_RIGHT_VIEWS: (state, view) => {  
      const visitedViewsIndex = state.visitedViews.findIndex(v => v.path === view.path);  
      const cachedViewsIndex = state.cachedViews.findIndex(i => i === view.name);  
      
      if (visitedViewsIndex !== -1) {  
        state.visitedViews = state.visitedViews.slice(0, visitedViewsIndex + 1);  
      }  
      
      if (cachedViewsIndex !== -1) {  
        state.cachedViews = state.cachedViews.slice(0, cachedViewsIndex + 1);  
      }  
      
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews));  
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews));  
    },
    DEL_ALL_VIEWS: (state) => {
      state.visitedViews = []
      state.cachedViews = []
      state.oldVisitedViews = []
      state.oldCachedViews = []
      sessionStorage.setItem('oldVisitedViews', JSON.stringify(state.oldVisitedViews))
      sessionStorage.setItem('oldCachedViews', JSON.stringify(state.oldCachedViews))
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    },
    CLONE_ALL_VIEWS: (state) => {
      state.oldVisitedViews = [...state.visitedViews]
      state.oldCachedViews = [...state.cachedViews]
      sessionStorage.setItem('oldVisitedViews', JSON.stringify(state.oldVisitedViews))
      sessionStorage.setItem('oldCachedViews', JSON.stringify(state.oldCachedViews))
    },
    PAST_ALL_VIEWS: (state, viewObj) => {
      state.visitedViews = [...viewObj.oldVisitedViews]
      state.cachedViews = [...viewObj.oldCachedViews]
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
      sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    },
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delLeftViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_LEFT_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delRightViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_RIGHT_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews({ commit, state }) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    },
    cloneAllViews({ commit }) {
      commit('CLONE_ALL_VIEWS')
    },
    pastAllViews({ commit }, viewObj) {
      commit('PAST_ALL_VIEWS', viewObj)
    }
  }
}

export default tagsView
